<?
include_once 'CoreModel.php';
Class ImportPromotionFMatModel extends CoreModel {
	private $table;
	public function __construct() {
		$this -> table = "promotion_fmat";
		parent::__construct();
	}

	public function insertConnectData($result) {
		/* $result is result from sap */
		//	print_r($result);
		//exit();
		// default log message
		$data_log['log_function'] = "ZSDRFC_SALESPROMO_DATA";
		$data_log['log_table'] = "PROMOTION_FMAT";
		$data_log['log_record'] = 0;
		$data_log['log_type'] = "INSERT";
		$data_log['log_status'] = "COMPLETED";
		$data_log['log_by_name'] = "Batch Job";
		$data_log['log_page'] = "adminpanel/importpromotionfmat";

		$numRows = 0;
		// start begin trans
		$this -> db -> trans_begin();
		$this -> db -> query('delete from PROMOTION_FMAT');
		$this->db->query("SET NAMES tis620"); 
		$dataInsert = array();
		
		foreach ($result["PROMOTION_FMAT"] as $pro) {
			$data = array();
			$data['PROMO_ID'] = $pro["PROMO_ID"];
			$data['MATNR'] = $pro["MATNR"];
			$dataInsert[] = $data;
		}

		// insert into SALES_DATA;
		$this -> db -> insert_batch($this -> table, $dataInsert);

		if ($this -> db -> trans_status() === FALSE) {
			// if error on tranaction
			$this -> db -> trans_rollback();
				
			// insert log  table & page
			$this->insertLogFailed('ZSDRFC_SALESPROMO_DATA', 'PROMOTION_FMAT', 'adminpanel/importpromotionfmat');
			
			return array("flag" => false, "message" => "error on transfer");
		} else {
			// when complete
			$this -> db -> trans_commit();
			$this->insertLogComplete('ZSDRFC_SALESPROMO_DATA', 'PROMOTION_FMAT', count($dataInsert), 'adminpanel/importpromotionfmat');
			return array("flag" => true, "connect_row" => count($dataInsert));
		}
	}

}
?>